<?php

namespace app\speedDating\model;

use app\speedDating\model\traits\SimpleModel;
use app\speedDating\services\common\UtilsService;
use think\Model;

class SddUser extends Model
{

    use SimpleModel;

    protected $connection = 'mysql';

    protected $pk = 'id';

    protected $name = 'sdd_user';

    protected static $createTimeField = 'gmt_create';

    protected static $updateTimeField = 'gmt_modified';

    /**
     * @see SimpleModel
     * @var string[] 字段数组
     */
    protected static $SimpleMainField = [
        'id',
        'nickname',
        'username',
        'picture',
        'inviter_id',
        'is_banned'
    ];


    /**
     * 创建会员账户
     * @param $data
     * @return mixed
     */
    public static function createUser($data)
    {
        $insData['username'] = $data['username'];
        $insData['password'] = $data['password'];
        $insData['nickname'] = $data['nickname'];
        $insData['inviter_id'] = $data['inviter_id'];

        return self::toInsert($insData);
    }

    /**
     * 根据ID查询会员
     * @param $id
     * @return User|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getInfoById($id)
    {
        return self::find($id);
    }

    /**
     * 根据手机号查询会员
     * @param $username
     * @return User|array|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getInfoByUsername($username)
    {
        return self::where('username', $username)->find();
    }

    #获取器修改
    public function getPictureAttr($value)
    {
        if (empty($value)) return $value;
        return UtilsService::domain() . $value;
    }
}

